/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.belfalas.simy86.gui;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import org.belfalas.simy86.Registradores;

/**
 *
 * @author belfalas
 */
public class window extends Component {
    
    private static int x = 5;
    private static int y = 57;
    private static int player_height = 40;
    private static int player_1_y = 240;
    private static int player_2_y = 240;
    private static boolean bool_x = false;
    private static boolean bool_y = false;
    private static double ball_y_movement = 5;
    public static Timer timer;

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, 501, 501);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, player_1_y, 5, player_height);
        g2d.drawString("x:" + Registradores.ECX.reg + " y:" + Registradores.EAX.reg, 420, 490);
        g2d.fillRect(495, y-(player_height/2), 5, player_height);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(Registradores.ECX.reg, Registradores.EAX.reg, 5, 5);
}

    public void start() {
        this.setSize(500, 500);
        this.setLocation(0, 0);
        JFrame frame = new JFrame();
        frame.setSize(550, 550);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(this);
        
        frame.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {
            }

            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if (player_1_y <= 500 - player_height - 10) {
                        player_1_y += 10;
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if (player_1_y >= 10) {
                        player_1_y -= 10;
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_R){
                    x=5;
                    y=player_1_y;
                }
            }

            public void keyReleased(KeyEvent e) {
            }
        });

        frame.addWindowListener(new WindowListener(){

            public void windowOpened(WindowEvent e) {
                
            }

            public void windowClosing(WindowEvent e) {
                timer.stop();
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }
            
        });
        
        this.setVisible(true);
        frame.setVisible(true);
        

    }

//    private static void moveX() {
//
//        if (x >= 500) {
//            bool_x = !bool_x;
//        }
//
//        if (x <= 5 && (player_1_y <= y && player_1_y + player_height >= y)) {
//            bool_x = !bool_x;
//            x = 5;
//            
//            double temp_x = (y - (player_1_y+(player_height/2)));
//            double angle = (double)(20 - 0.2*(temp_x*temp_x));
//            //System.out.println(player_1_y);
//            //System.out.println(y);
//            System.out.println("y="+y);
//            System.out.println("player_y="+player_1_y);
//            System.out.println("my_y="+(player_1_y+(player_height/2)));
//            System.out.println(temp_x);
//            System.out.println(angle+70);
//            angle += 70.0;
//            angle = Math.toRadians(Math.abs(angle));
//            System.out.println(angle);
//            ball_y_movement = 5*Math.sin(angle);
//            System.out.println(ball_y_movement);
//        }
//
//        if (!bool_x) {
//            x += 5;
//        } else {
//            x -= 5;
//        }
//    }
    public void moveY() {       

        if (y >= 500) {
            bool_y = !bool_y;
            y = 495;
        }
        if (y <= 0){
            bool_y = !bool_y;
            y=5;
        }
        
        if (!bool_y) {
            y += ball_y_movement;
        } else {
            y -= ball_y_movement;
        }      
        
    }
    
    public void redraw(){
        this.repaint();
    }
}
